package com.arm.tools;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by Baggio on 2018-9-13.
 */
public class AliUploadTools {
    /**
     * y云盘连接变量
     */
    private static final String ENDPOINT = "http://oss-cn-shenzhen.aliyuncs.com";
    private static final String ACCESS_ENDPOINT = "http://autumnrainmorning2.oss-cn-shenzhen.aliyuncs.com";
    private static final String ACCESSKEYID = "LTAII31XbXRP0G6f";
    private static final String ACCESSKEYSECRET = "E0mDm4GXvn8Z1yernbNd4RFk0Xf1We";
    public static final String PICURLADDRESS = "http://autumnrainmorning2.oss-cn-shenzhen.aliyuncs.com/";
    public static final String BUCKENTNAME = "autumnrainmorning2";

    private static InputStream bufferedImage2InputStream(BufferedImage bufferedImage) {
        //BufferedImage 转 InputStream
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
            ImageIO.write(bufferedImage, "jpeg", imageOutput);
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            return inputStream;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //outputStream转inputStream
    private static ByteArrayInputStream parse(OutputStream out) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }

    public static void AliUploadBufferedImage(BufferedImage bufferedImage, boolean flag, String key) throws IOException {
        InputStream content = bufferedImage2InputStream(bufferedImage);
        save2Ali(content, flag, key);
    }

    private static void save2Ali(InputStream inputStream, boolean flag, String key) { // 创建上传Object的Metadata
        // 必须设置ContentLength
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("image/jpeg");
        meta.setCacheControl("max-age=8640000");
        // meta.setContentLength(file.getSize());
        OSSClient ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
        try {
            OutputStream out = new ByteArrayOutputStream();
            Thumbnails.of(inputStream).scale(1f).outputQuality(0.25f).toOutputStream(out);

            InputStream in = parse(out);
            int b = in.available();
            meta.setContentLength(b);
            // 上传Object.
            ossClient.putObject(BUCKENTNAME, key, in, meta);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void AliUploadInputStream(InputStream inputStream, boolean flag, String key) throws IOException {
        save2Ali(inputStream, flag, key);
    }

    /**
     *
     * @MethodName: putObject
     * @Description: 上传文件
     * @param file
     * @param fileType
     * @param fileName
     * @return String
     */
    public static String putObject(InputStream inputStream,String fileType,String fileName){
        String url = null;		//默认null
        OSSClient ossClient = null;
        try {
              ossClient = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
            ObjectMetadata meta = new ObjectMetadata();				// 创建上传Object的Metadata
            meta.setContentType(AliUploadTools.contentType(fileType));		// 设置上传内容类型
            meta.setCacheControl("no-cache");					// 被下载时网页的缓存行为
            PutObjectRequest request = new PutObjectRequest(BUCKENTNAME, fileName,inputStream,meta);			//创建上传请求
            ossClient.putObject(request);
            url = PICURLADDRESS+"/"+fileName+"."+fileType;		//上传成功再返回的文件路径
        } catch (OSSException oe) {
            oe.printStackTrace();
            return null;
        } catch (ClientException ce) {
            ce.printStackTrace();
            return null;
        }  finally {
            ossClient.shutdown();
        }
        return url;
    }

    /**
     *
     * @MethodName: contentType
     * @Description: 获取文件类型
     * @param fileType
     * @return String
     */
    private static String contentType(String fileType){
        fileType = fileType.toLowerCase();
        String contentType = "";
        switch (fileType) {
            case "bmp":	contentType = "image/bmp";
                break;
            case "gif":	contentType = "image/gif";
                break;
            case "png":
            case "jpeg":
            case "jpg":	contentType = "image/jpeg";
                break;
            case "html":contentType = "text/html";
                break;
            case "txt":	contentType = "text/plain";
                break;
            case "vsd":	contentType = "application/vnd.visio";
                break;
            case "ppt":
            case "pptx":contentType = "application/vnd.ms-powerpoint";
                break;
            case "doc":
            case "docx":contentType = "application/msword";
                break;
            case "xml":contentType = "text/xml";
                break;
            case "mp4":contentType = "video/mp4";
                break;
            default: contentType = "application/octet-stream";
                break;
        }
        return contentType;
    }


}
